<?php


namespace app\user\controller;

use app\common\controller\Common;
use app\common\model\Cards;
use app\common\model\ReferralCode;
use app\common\model\Smscode;
use app\common\model\Users;
use asms\Alisms;
use think\Db;

class Member extends Common
{

    public function ttt(){
        $r=1;
        if ($r!=0){
            dump('1');
        }
        dump('2');
    }

    public function index()
    {//返回会员信息
        $openid = request()->param('openid');
        if ($openid) {
            $reMem = \app\common\model\Member::field('memid,name,mobile,sex,birthday,cards_nember,status')->where('openid', '=', $openid)->find();

            if (empty($reMem)) return $this->errJson('无此会员信息！');
            return $this->okJson('查询成功', $reMem);
        }
        return $this->errJson('缺少参数！');

    }

    public function getMemberCards()
    {//返回会员卡的信息
        $openid = request()->param('openid');
        if (is_null($openid) || $openid == "") return $this->errJson('缺少参数！');
        $remMem = \app\common\model\Member::where('openid', '=', $openid)->field('memid')->find();
        if ($remMem) {
            $memid = $remMem->memid;
            $reCards = Cards::field('cardid,cate_title,card_no,term,status,use_time')->where('memid', '=', $memid)->find();
            if ($reCards) {
                $Cards = $reCards->toArray();
                $Cards['endtime'] = date("Y-m-d", strtotime("+" . $Cards['term'] . " month", $Cards['use_time']));
                $Cards['use_time'] = date("Y-m-d", $Cards['use_time']);
                return $this->okJson('ok', $Cards);
            }
            return $this->errJson('您还未开通会员卡');

        }
        return $this->errJson('缺少参数！');
    }


    //注册
    public function register()
    {
        $mobile = request()->param('mobile');
        $vcode = request()->param('vcode');
        $openid = request()->param('openid');
        $ref_code = request()->param('ref_code');
        if ($mobile == "" || $mobile == null) return $this->errJson('请输入手机号');
        if ($vcode == "" || $vcode == null) return $this->errJson('请输入验证码');
        if ($openid == "" || $openid == null) return $this->errJson('缺少参数！');
        $ret = Smscode::where(['mobile' => trim($mobile), 'code' => trim($vcode)])->order('time', 'desc')->find();
        if ($ret) {
            $dtime = time() - $ret->time;
            if ($dtime / 60 > 12) return $this->errJson('验证码超时');

            $Member = new \app\common\model\Member();
            $resMe = $Member->where('openid', '=', $openid)->find();
            if ($resMe) return $this->errJson('您已注册过会员了！');
            $User = new Users();
            $retUser = $User->where('openid', '=', $openid)->find();
            if (empty($retUser)) return $this->errJson('参数错误');
//            Db::startTrans();
//            try{
            if (!is_null($ref_code) || $ref_code != "") {
                $reRef = ReferralCode::where(['ref_code' => $ref_code, 'status' => -1])->find();//查找状态为未使用的推荐码，
                if (empty($reRef)) return $this->errJson('参数错误');
                $reRef->status = 1;
                $reRef->reg_time = time();
            } else {
                $flag=true;
                $ref_code = 0;

            }
            $data = [
                'mobile' => $mobile,
                'openid' => $openid,
                'name' => $retUser->username,
                'status' => -1,
                'ref_code' => $ref_code,
                'retime' => time(),
                'update' => time(),
            ];
                $res = \app\common\model\Member::create($data);
                if (!is_null($ref_code)&&$ref_code!=0){
                    $reRef->memid=$res->memid;
                    if($reRef->save()) {
                        $flag=true;
                    }else{
                        $flag=false;
                    }
                }
                if ($res&&$flag){
//                    Db::commit();
                    return $this->okJson('注册成功');
                }
                return $this->errJson('注册失败');
//            }catch(\Exception $exception){
//                Db::rollback();
//                return $this->errJson('注册失败->'.$exception);
//            }

        }
        return $this->errJson('手机号或验证码有误');

    }

    //发送验码
    public function sendSms()
    {
        $mobile = input('mobile');
        if ($mobile == "" || $mobile == null) return $this->errJson('请输入手机号');
        $reSms = Smscode::where('mobile', '=', $mobile)->order('time', 'desc')->find();
        if (!empty($reSms)) {
            $dtime = time() - $reSms->time;
            if ($dtime < 60) return $this->errJson('60秒内只发送一次');
        }
        $sms = new Alisms();
        $ret = $sms->send($mobile);
        if ($ret['Code'] == 'OK') {
            $Smscode = new Smscode();
            $res = $Smscode->save([
                'mobile' => $mobile,
                'code' => $ret['code'],
                'time' => time()
            ]);
            if ($res) return $this->okJson('短信已发送');
        }
        return $this->errJson($ret['Message']);
    }

    public function getMembervcode()
    {
        $openid = request()->param('openid');
        $cardid = request()->param('cardid');
        if (is_null($openid) || $openid == "") return $this->errJson('缺少参数！');
        if (is_null($cardid) || $cardid == "") return $this->errJson('缺少参数！');
        $Cards = new Cards();
        $reCards = $Cards->where('cardid', '=', $cardid)->find();
        if (empty($reCards)) return $this->errJson('未找到相应的会员卡信息');
        $reMem = \app\common\model\Member::field('memid')->where('openid', '=', $openid)->find();
        if ($reMem) {
            $memid = $reMem->memid;
            $sai = config('app.sai');
            $scode = $this->setToken();
            $scode = substr($scode, 0, 5);
            $vcode = $cardid . $sai . '|' . $memid . '*' . time();
            $membervcode = $scode . base64_encode($vcode);
            return $this->okJson('ok', $membervcode);
        }
        return $this->errJson('数据异常');
    }

    //会员信息编辑
    public function edit()
    {
        $memid = request()->param('memid');
        if ($memid == "" || $memid == null) return $this->errJson('缺少参数！');
        $key = request()->param('key');
        $k = substr($key, 0, 1);
        $v = substr($key, 1);
        $mem = \app\common\model\Member::get($memid);
        switch ($k) {
            case 'b':
                $mem->birthday = $v;
                break;
            case 'n':
                $mem->name = $v;
                break;
            case 's':
                $mem->sex = $v;
                break;
        }
        $ret = $mem->save();
        if ($ret) return $this->okJson('修改成功');
    }


}


